/**
 * ProductController
 *
 * @description :: Server-side logic for managing products
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */

module.exports = {

  index: function (req, res) {
    Product.find().then(function (record) {
      Category.find().then(function (cats) {
        res.view('product', {data: record, category: cats});
      }).catch(function (err) {
        res.serverError(err);
      })
    }).catch(function (err) {
      res.serverError(err);
    })
  },

  detail: function (req, res) {
    var productId = req.params.id;
    Product
    return res.view('detail', {id: productId});
  },

  category: function (req, res) {
    Category.find().then(function (records) {
      res.view('admin/category', {layout: 'admin/layout', data: records});
    })
  },

  addCat: function (req, res) {
    var name = req.body.cat_name;
    Category.create({category_name: name}).then(function (record) {
      console.log(record);
      res.redirect('/admin/product/cat');
    }).catch(function (err) {
      res.serverError(err);
    })
  },

  deleteCat: function (req, res) {
    var id = req.params.id;
    Category.destroy([id]).exec(function (err, record) {
      if (err) return res.serverError(err);
      res.send({data: record, status: 'success'});
    })
  },

  getCat: function (req, res) {
    Category.find().then(function (record) {
      res.send({data: record, status: 'success'});
    }).catch(function (err) {
      res.serverError(err);
    })
  },

  all: function (req, res) {
    var category = req.params.id;
    if (category == 'all') {
      Product.find().then(function (record) {
        res.view('admin/product', {data: record, layout: 'admin/layout'});
      })
    } else {
      Category.find({id: category}).then(function (record) {
        Product.find({cat: record[0].category_name}).then(function (record) {
          res.view('admin/product', {data: record, layout: 'admin/layout'});
        })
      }).catch(function (err) {
        res.serverError(err);
      })
    }
  },

  addProduct: function (req, res) {
    var productName = req.body.p_name;
    var productSpec = req.body.p_spec;
    var productInfo = req.body.p_info;
    var productCat = req.body.p_cat;
    var img = req.file('p_img');
    img.upload({dirname: require('path').resolve(sails.config.appPath, 'assets/img')}, function (err, uploadedFiles) {
      if (err) return res.serverError(err);
      var arr = uploadedFiles[0].fd.split('/');
      var imgName = arr[arr.length-1];
      Product.create({product_name: productName, specification: productSpec, info: productInfo, cat: productCat, img: imgName}).then(function (record) {
        console.log(record);
        res.redirect('/admin/product/list/all');
      })
    })
  },

  deleteProduct: function (req, res) {
    var id = req.params.id;
    Product.destroy([id]).exec(function (err, record) {
      if (err) return res.serverError(err);
      console.log(record);
      res.redirect('/admin/product/list/all');
    })
  }
};

